__author__="Yakovenko Kirill"
__date__ ="$08.05.2009$"

import wx
import win32con

class MainWindow(wx.Frame):
    isStarted = False

    TBMENU_SHOW = 1000
    TBMENU_START = 1001
    TBMENU_STOP = 1002
    TBMENU_CLOSE = 1003
    HOTKEY_HANDLER = 1004

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
        self.__loadIcons()
        self.__createWindow()
        self.__createTray()

    def __createTray(self):
        self.__trayIcon = wx.TaskBarIcon()
        wx.EVT_TASKBAR_LEFT_DCLICK(self.__trayIcon, self.onTaskBarActivate)
        wx.EVT_TASKBAR_RIGHT_UP(self.__trayIcon, self.onTaskBarMenu)
        wx.EVT_MENU(self.__trayIcon, self.TBMENU_SHOW, self.visible)
        wx.EVT_MENU(self.__trayIcon, self.TBMENU_START, self.start)
        wx.EVT_MENU(self.__trayIcon, self.TBMENU_STOP, self.stop)
        wx.EVT_MENU(self.__trayIcon, self.TBMENU_CLOSE, self.onTaskBarClose)

    def visible(self, event = None):
        self.Show(True)
        self.__trayIcon.RemoveIcon()

    def onTaskBarClose(self, event):
        self.Close()

    def onTaskBarMenu(self, evt):
        menu = wx.Menu()
        menu.Append(self.TBMENU_SHOW, self.__loginText.GetLabelText())
        if self.isStarted:
            menu.Append(self.TBMENU_STOP, "&Stop")
        else:
            menu.Append(self.TBMENU_START, "&Start")
        menu.AppendSeparator()
        menu.Append(self.TBMENU_CLOSE,   "&Close")
        self.__trayIcon.PopupMenu(menu)
        menu.Destroy()

    def onTaskBarActivate(self, evt):
        if not self.IsShown():
            self.Show(True)

    def __createWindow(self):
        pane = wx.FlexGridSizer(cols = 2)
        self.SetIcon(self.__disableIcon)
        pane.AddGrowableCol(1)
        pane.Add(wx.StaticText(self, -1, "Login:"), flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT, border = 1)
        self.__loginText = wx.TextCtrl(self, wx.NewId(), "Your login here")
        pane.Add(self.__loginText, flag = wx.EXPAND)
        pane.Add(wx.StaticText(self, -1, "Password:"), flag = wx.ALIGN_CENTER_VERTICAL | wx.LEFT, border = 1)
        self.__passwordText = wx.TextCtrl(self, wx.NewId(), "Your password here")
        pane.Add(self.__passwordText, flag = wx.EXPAND)
        pane.Add(wx.StaticText(self, -1, ""))
        buttonsSizer = wx.BoxSizer()
        self.__startButton = wx.Button(self, wx.NewId(), "Start")
        self.__stopButton = wx.Button(self, wx.NewId(), "Stop")
        self.__stopButton.Enable(False)
        buttonsSizer.Add(self.__startButton, flag = wx.TOP | wx.RIGHT, border = 2)
        buttonsSizer.Add(self.__stopButton, flag = wx.TOP | wx.LEFT, border = 2)
        wx.EVT_BUTTON(self.__startButton, self.__startButton.Id, self.start)
        wx.EVT_BUTTON(self.__stopButton, self.__stopButton.Id, self.stop)
        self.Bind(wx.EVT_HOTKEY, self.onHotKeyActivate)
        pane.Add(buttonsSizer, flag = wx.ALIGN_RIGHT)
        self.SetSizer(pane)
        self.SetSize((350, 105))
        self.Centre()
        self.Layout()
    
    def onHotKeyActivate(self, event):
        bitmap = self.getScreenshotBitmap()
        bitmap.SaveFile("s.bmp", wx.BITMAP_TYPE_BMP)
    def getScreenshotBitmap(self):
        p = wx.GetDisplaySize()
        bitmap = wx.EmptyBitmap( p.x, p.y)
        dc = wx.ScreenDC()
        memdc = wx.MemoryDC()
        memdc.SelectObject(bitmap)
        memdc.Blit(0,0, p.x, p.y, dc, 0,0)
        memdc.SelectObject(wx.NullBitmap)
        return bitmap

    def start(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_WATCH))
        self.Enable(False)
        
        #test net and start service
        #add message in tray
        self.SetCursor(wx.NullCursor)
        self.Enable(True)
        self.__loginText.Enable(False)
        self.__passwordText.Enable(False)
        self.__startButton.Enable(False)
        self.__stopButton.Enable(True)
        self.__trayIcon.SetIcon(self.__enableIcon)
        self.isStarted = True
        print self.RegisterHotKey(self.HOTKEY_HANDLER, 0, win32con.VK_F11)
        self.Show(False)

    def stop(self, event):
        self.__loginText.Enable(True)
        self.__passwordText.Enable(True)
        self.__startButton.Enable(True)
        self.__stopButton.Enable(False)
        if not self.IsShown():
            self.__trayIcon.SetIcon(self.__disableIcon)
        self.isStarted= False
        self.UnregisterHotKey(self.HOTKEY_HANDLER)
        
    def __loadIcons(self):
         self.__enableIcon = wx.Icon("images/enable.ico", wx.BITMAP_TYPE_ICO)
         self.__disableIcon = wx.Icon("images/disable.ico", wx.BITMAP_TYPE_ICO)

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    mainWnd = MainWindow(None, -1, "Autorization")
    mainWnd.Show(True)
    app.MainLoop()